using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace HW2and3
{
    class Controller : ControllerInterface
    {
        private ModelBase model;
        private ViewInterface textView;
        private ViewInterface guiView;
        private string[] titles = { "Knight's Tour Problem", "Knight's Tour Problem, look ahead version", "N Queens Problem", "N Queens Problem, look ahead version" };
        private string defaultTitle = "Knight's Tour Problem/N Queens Problem";

        public Controller()
        {
            ///Obtener el style de winxp
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            textView = new ViewTextMode();
            
            guiView = new ViewGUI();

            textView.Initialize(defaultTitle, this);
            guiView.Initialize(defaultTitle, this);

            ///lanzar el guiView de manera persistente
            //try
            //{
                Application.Run((Form)guiView);
            //}
            //catch (Exception e)
            //{
            //    MessageBox.Show("Error executing main form.");
            //}
        }

        public void SetModel(Behaviors.Behavior behavior, int sleepInterval, int size, int x, int y)
        {
            //Escogemos que modelo vamos a usar dependiendo del behavior
            int i = 0;
            if (behavior == Behaviors.Behavior.KTDFS)
            {
                model = new KTModelDFS();
            }
            else if (behavior == Behaviors.Behavior.KTLA)
            {
                model = new KTModelDFSLookAhead();
                i = 1;
            }
            else if (behavior == Behaviors.Behavior.NQBFS)
            {
                model = new NQModelBFS();
                i = 2;
            }
            else if (behavior == Behaviors.Behavior.NQLA)
            {
                model = new NQModelBFSLookAhead();
                i = 3;
            }

            model.Initialize(sleepInterval, size, x, y);
            textView.SetModel(titles[i], model);
            guiView.SetModel(titles[i], model);
        }

        public void Start()
        {
            model.Start();
        }

        public void Pause()
        {
            model.Pause();
        }

        public void Stop()
        {
            model.Destroy();
        }

        public void SetSleepInterval(int sleepInterval)
        {
            model.SleepInterval = sleepInterval;
        }

        public bool IsRunning()
        {
            return model.IsRunning;
        }

        public bool IsStopped()
        {
            return model.IsStopped;
        }
    }
}
